package com.inei.cie.utilitarios;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewDebug.CapturedViewProperty;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.inei.cie.R;
import com.inei.cie.capitulov.CapituloV_Pisos;
import com.inei.cie.capitulov.CapituloV_Total_Ambientes_Activity;
import com.inei.cie.dao.CapituloVDao;
import com.inei.cie.entidades.P5;
import com.inei.cie.entidades.P5_F;
import com.inei.cie.entidades.P5_N;

public class ListadoAmbientesPorEdificacionAdapter extends BaseAdapter {
	Context context;
	public ArrayList<String> listaAmbientes;
	private String codLocal;
	private int numeroPredio;
	private CapituloVDao cap5Dao;
	public P5 obj2;
	public int numeroPiso;
	public int count;
	public List<P5_N> listObj;

	public ListadoAmbientesPorEdificacionAdapter(Context context,
			String codLocal, int numeroPredio, int numeroPiso) {
		cap5Dao = new CapituloVDao(context);
		this.codLocal = codLocal;
		this.numeroPredio = numeroPredio;
		this.context = context;
		this.numeroPiso = numeroPiso;
		listaAmbientes = new ArrayList<String>();
		obj2 = cap5Dao.obtenerP5Local(codLocal, numeroPredio);
		count = obj2.getP5_Tot_E();
		cap5Dao.crearP5_NEdificaciones(codLocal, numeroPredio, numeroPiso,
				count);
		listObj = cap5Dao.obtenerP5_NLocal(codLocal, numeroPredio, numeroPiso);
		for (int i = 0; i < count; i++) {
			if (i < listObj.size()) {
				P5_N buscar = listObj.get(i);
				if (buscar.getP5_TotAmb() < 10) {
					listaAmbientes.add(buscar.getP5_TotAmb() != 0 ? "0"
							+ buscar.getP5_TotAmb() + "" : "");
				} else {
					listaAmbientes.add(buscar.getP5_TotAmb() != 0 ? buscar
							.getP5_TotAmb() + "" : "");
				}
			} else {
				listaAmbientes.add("");
			}
		}
	}

	@Override
	public int getCount() {
		return listaAmbientes.size();
	}

	@Override
	public Object getItem(int pos) {
		return listaAmbientes.get(pos);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int pos, View view, ViewGroup grupo) {
		View item = view;
		final ViewHolder holder;
		if (item == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			item = inflater.inflate(R.layout.item_lista_ambientes, null);
			holder = new ViewHolder();
			holder.tviAmbiente = (TextView) item
					.findViewById(R.id.tviAmbientes);

			holder.eteAmbiente = (EditText) item
					.findViewById(R.id.eteAmbientes);
			holder.eteAmbiente.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
					// TODO Auto-generated method stub

				}

				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {
					// TODO Auto-generated method stub

				}

				@Override
				public void afterTextChanged(Editable s) {
					// TODO Auto-generated method stub

				}
			});

			item.setTag(holder);
		} else {
			holder = (ViewHolder) item.getTag();
		}
		holder.tviAmbiente.setText("Edificacion Nro "
				+ ((pos + 1) < 10 ? "0" : "") + (pos + 1));
		holder.eteAmbiente.setText(listaAmbientes.get(pos));
		// notifyDataSetChanged();

		return item;
	}

	public class ViewHolder {
		TextView tviAmbiente;
		EditText eteAmbiente;
	}
}
